/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成Action实现类
 */
public class ControllerTempManager extends SupperTemplateManager {

	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		List<Map<String, Object>> list = (List<Map<String, Object>>) entity.getParam().get("sortList");
		String jarPath = entity.getPackPath();
		String prefix = entity.getProjectName() + "/src/main/";
		String xmlPath = entity.getFilePath() + "/" + prefix + "java/" + jarPath.replace(".", "/")
				+ "/controller/";
		String sysName = entity.getSysEname();
		FileOperate.getInstance().newCreateFolder(xmlPath);
		String restfullClassName = "Restfull" + GZipUtil.toUpperFirst(sysName) + "Action";
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("restfullClassName", restfullClassName);
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		try {
			String result = FreemarkerHelper.instance().getTemplateStr("controller/RestfullAction.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + restfullClassName + ".java", result);
			result = FreemarkerHelper.instance().getTemplateStr("controller/TransAction.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "TransAction.java", result);
			if(entity.isCreateAction()) {
				for (Map<String, Object> map : list) {
					String reqObjName = GZipUtil.toCamelNamed(String.valueOf(map.get("sortCode")));
					data.put("reqObjName", reqObjName);
					data.put("sort", map);
					result = FreemarkerHelper.instance().getTemplateStr("controller/ObjAction.inc", data);
					FileOperate.getInstance()
							.newCreateFile(xmlPath + String.valueOf(map.get("objName")) + "Action.java",
									result);
				}
				result = FreemarkerHelper.instance().getTemplateStr("controller/SuperAction.inc", data);
				FileOperate.getInstance().newCreateFile(xmlPath + "UnitySupperAction.java", result);
			}
			mark = true;
		} catch (Exception e) {
			log.error("生成action失败", e);
		}
		return mark;
	}
}
